package com.yunkazaixian.syntelecom.service;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunkazaixian.syntelecom.entity.*;
import com.yunkazaixian.syntelecom.mapper.McSimDxMapper;
import com.yunkazaixian.syntelecom.utils.*;
import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Service
public class DXService {

    @Autowired
    XMLSerializer xmlSerializer;

    @Autowired
    McSimDxMapper simDxMapper;


    //停机保号
    @GetMapping("/disabledNumberService")
    @ResponseBody
    public HttpResponse disabledNumberService(McSimDx simDx) {
        Map<String, String> map = new HashMap<>();
        map.put("access_number", simDx.getTelephone());
        map.put("orderTypeId", "19");
        ComRes bean = null;
        try {
            String s = DXUtils.get(DXConstants.disabledNumberService, simDx.getAppkey(), map, String.class);
            JSON read = xmlSerializer.read(s);
            bean = FastJsonUtils.fromJson(read.toString(), ComRes.class);
            System.out.println( "------" + bean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (bean != null) {
            if ("0".equals(bean.getResult())) {
                return R.ok("发送成功");
            } else {
                return R.fail(bean.getResultMsg());
            }
        }
        return R.fail("第三方返回异常");
    }

    //21表示去激活停机
    @GetMapping("/disabledNumberService2")
    @ResponseBody
    public HttpResponse disabledNumberService2(McSimDx simDx) {
        Map<String, String> map = new HashMap<>();
        map.put("access_number", simDx.getTelephone());
        map.put("orderTypeId", "21");
        ComRes bean = null;
        try {
            String s = DXUtils.get(DXConstants.disabledNumberService, simDx.getAppkey(), map, String.class);
            JSON read = xmlSerializer.read(s);
            bean = FastJsonUtils.fromJson(read.toString(), ComRes.class);
            System.out.println( "------" + bean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (bean != null) {
            if ("0".equals(bean.getResult())) {
                return R.ok("发送成功");
            } else {
                return R.fail(bean.getResultMsg());
            }
        }
        return R.fail("第三方返回异常");
    }

    //复机
    @GetMapping("/enabledNumberService")
    @ResponseBody
    public HttpResponse enabledNumberService(String iccid) {
        McSimDx simDx = simDxMapper.selectOne(new LambdaQueryWrapper<McSimDx>().eq(McSimDx::getIccid, iccid));
        Map<String, String> map = new HashMap<>();
        map.put("access_number", simDx.getTelephone());
        map.put("orderTypeId", "20");
        ComRes bean = null;
        try {
            String s = DXUtils.get(DXConstants.disabledNumberService, simDx.getAppkey(), map, String.class);
            JSON read = xmlSerializer.read(s);
            bean = FastJsonUtils.fromJson(read.toString(), ComRes.class);
            System.out.println(iccid + "------" + bean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (bean != null) {
            if ("0".equals(bean.getResult())) {
                return R.ok("发送成功");
            } else {
                return R.fail(bean.getResultMsg());
            }
        }
        return R.fail("第三方返回异常");
    }

    @GetMapping("/queryPakage")
    @ResponseBody
    public Double queryPakage(String iccid, String monthDate) throws Exception {


        McSimDx simDx = simDxMapper.selectOne(new LambdaQueryWrapper<McSimDx>().eq(McSimDx::getIccid, iccid));
        Map<String, String> map = new HashMap<>();
        map.put("access_number", simDx.getTelephone());
        map.put("monthDate", monthDate);
        String s = DXUtils.get(DXConstants.queryPakage, simDx.getAppkey(), map, String.class);
        JSON read = xmlSerializer.read(s);
        QueryPakageBean queryPakageBean = FastJsonUtils.fromJson(read.toString(), QueryPakageBean.class);
        System.out.println(queryPakageBean);
        Double flow = 0.0;
        if (queryPakageBean != null && queryPakageBean.getWeb() != null && "0".equals(queryPakageBean.getWeb().getIresult())) {
            if (!ListUtils.isEmpty(queryPakageBean.getWeb().getCumulRspList())) {//MB
                for (QueryPakageBean.CumulRsp cumulRsp : queryPakageBean.getWeb().getCumulRspList()) {
                    if ("MB".equals(cumulRsp.getUnit_name())) {
                        flow += Double.parseDouble(cumulRsp.getCumulation_already());
                    }
                }
                return flow;
            }
            return flow;
        }
        return flow;

    }

    @GetMapping("/queryTrafficByDate")
    @ResponseBody
    public String queryTrafficByDate(String iccid, String startDate, String endDate) {
        try {
            McSimDx simDx = simDxMapper.selectOne(new LambdaQueryWrapper<McSimDx>().eq(McSimDx::getIccid, iccid));
            Map<String, String> map = new HashMap<>();
            map.put("access_number", simDx.getTelephone());
            map.put("needDtl", "0");
            map.put("startDate", startDate);
            map.put("endDate", endDate);
            String s = DXUtils.get(DXConstants.queryTrafficByDate, simDx.getAppkey(), map, String.class);
            JSON read = xmlSerializer.read(s);
            QueryTrafficByDateBean bean = FastJsonUtils.fromJson(read.toString(), QueryTrafficByDateBean.class);
            System.out.println(bean);
            if (bean != null && bean.getWeb() != null && "0".equals(bean.getWeb().getIresult())) {
                return bean.getWeb().getTotalBytesCNT();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return "0.0";
    }

    public String queryTrafficDetailByDate(String iccid, String startDate, String endDate) {
        try {

            McSimDx simDx = simDxMapper.selectOne(new LambdaQueryWrapper<McSimDx>().eq(McSimDx::getIccid, iccid));
            Map<String, String> map = new HashMap<>();
            map.put("access_number", simDx.getTelephone());
            map.put("needDtl", "1");
            map.put("startDate", startDate);
            map.put("endDate", endDate);
            String s = DXUtils.get(DXConstants.queryTrafficByDate, simDx.getAppkey(), map, String.class);
            JSON read = xmlSerializer.read(s);
            QueryTrafficByDateBean bean = FastJsonUtils.fromJson(read.toString(), QueryTrafficByDateBean.class);
            System.out.println(com.alibaba.fastjson.JSONObject.toJSONString(bean));
            if (bean != null && bean.getWeb() != null && "0".equals(bean.getWeb().getIresult()) && !ListUtils.isEmpty(bean.getWeb().getNEW_DATA_TICKET_QRlist())) {
                // return queryTrafficBean.getWeb().getTotalBytesCnt();
                String srcTime = bean.getWeb().getNEW_DATA_TICKET_QRlist().get(0).getStartTime();
                if (StringUtils.hasText(srcTime) && !"[]".equals(srcTime)) {
                    return srcTime.split(" ")[0].replaceAll("/", "");
                }
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
